package cn.kooki.app.duobao.ui.widget;

import android.animation.ValueAnimator;
import android.graphics.drawable.BitmapDrawable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.Index.GoodsItem;
import cn.kooki.app.duobao.util.StringUtils;

/**
 * Created by xiong on 2015/12/15.
 */
public class AddShopCartPopWin extends PopupWindow implements View.OnClickListener {
    @Bind(R.id.good_detail_close_button)
    ImageButton goodDetailCloseButton;
    @Bind(R.id.goodDetailNumberPicker)
    NumberPicker goodDetailNumberPicker;
    @Bind(R.id.mutiPeriodText)
    TextView mutiPeriodText;
    @Bind(R.id.goodDetailNumberPicker2)
    NumberPicker goodDetailNumberPicker2;
    @Bind(R.id.good_detail_confirm_button)
    Button goodDetailConfirmButton;
    @Bind(R.id.good_detail_cart_confirm_button)
    Button goodDetailCartConfirmButton;
    private AppCompatActivity context;
    private GoodsItem data;
    private boolean isduobao = true;

    private AddBtnClickListener addBtnClickListener;

    public void setAddBtnClickListener(AddBtnClickListener addBtnClickListener) {
        this.addBtnClickListener = addBtnClickListener;
    }
    public GoodsItem getData() {
        return data;
    }

    public interface AddBtnClickListener {
        void onClick(int index);
    }


    public AddShopCartPopWin(AppCompatActivity context) {
        this.context = context;
        initView();
    }

    public void setIsduobao(boolean isduobao) {
        this.isduobao = isduobao;
        refreshAddbtns();
    }

    private void initView() {
        View view = View.inflate(context, R.layout.gooddetail_popupwindow, null);
        ButterKnife.bind(this, view);
        setContentView(view);
        setBackgroundDrawable(new BitmapDrawable());
        setAnimationStyle(R.style.popUpAnimtation);
        setFocusable(true);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                animibgnormal();

            }
        });
        goodDetailCartConfirmButton.setOnClickListener(this);
        goodDetailConfirmButton.setOnClickListener(this);
        refreshAddbtns();
        goodDetailCloseButton.setOnClickListener(this);

    }

    private void refreshAddbtns() {
        if (isduobao) {
            goodDetailCartConfirmButton.setVisibility(View.GONE);
            goodDetailConfirmButton.setVisibility(View.VISIBLE);
        } else {
            goodDetailConfirmButton.setVisibility(View.GONE);
            goodDetailCartConfirmButton.setVisibility(View.VISIBLE);
        }
    }

    private void animibgnormal() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.7f, 1.0f);
        valueAnimator.setDuration(200);
        final WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                lp.alpha = value;
                context.getWindow().setAttributes(lp);
            }
        });
        // 设置背景颜色变暗

        valueAnimator.start();
    }

    private void animibgdark() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(1.0f, 0.7f);
        valueAnimator.setDuration(200);
        final WindowManager.LayoutParams lp = context.getWindow().getAttributes();

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                lp.alpha = value;
                context.getWindow().setAttributes(lp);
            }
        });
        valueAnimator.start();
    }

    public void showInView(View view) {
        animibgdark();
        ViewGroup viewGroup = findSuitableParent(view);
        showAtLocation(viewGroup, Gravity.BOTTOM, 0, 0);
    }


    private ViewGroup findSuitableParent(View view) {
        ViewGroup fallback = null;

        do {
            if (view instanceof CoordinatorLayout) {
                return (ViewGroup) view;
            }

            if (view instanceof FrameLayout) {

                fallback = (ViewGroup) view;
            }

            if (view != null) {
                ViewParent parent = view.getParent();
                view = parent instanceof View ? (View) parent : null;
            }
        } while (view != null);

        return fallback;
    }

    public void unbind() {
        ButterKnife.unbind(this);
    }

    public void setdata(final GoodsItem data) {
        this.data = data;

        int total = StringUtils.nullSafeParseInt(data.getZongrenshu());
        int currt = StringUtils.nullSafeParseInt(data.getCanyurenshu());
        goodDetailNumberPicker.setMaxCount(total - currt);
        goodDetailNumberPicker.setCurrtCount(StringUtils.nullSafeParseInt(data.getYunjiage()));
        goodDetailNumberPicker.setOnceCount(StringUtils.nullSafeParseInt(data.getYunjiage()));
        goodDetailNumberPicker.setChangerListener(new NumberPicker.OnNumberValueChangerListener() {
            @Override
            public void onChanged(int value) {
                data.count = value;
            }
        });

    }

    @Override
    public void onClick(View v) {
        if (v == goodDetailCartConfirmButton) {
            if (data != null) {
                dismiss();
                if (addBtnClickListener != null) {
                    addBtnClickListener.onClick(1);
                }
            }
        }
        if (v == goodDetailConfirmButton) {
            if (data != null) {
                dismiss();
                if (addBtnClickListener != null) {
                    addBtnClickListener.onClick(0);
                }
            }
        }
        if (v == goodDetailCloseButton) {
            dismiss();
        }
    }
}
